//
//  BHServiceEngine.h
//  s.
//
//  Created by fanshijian on 2022/8/23.
//  Copyright © 2022 J. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AFHTTPRequestOperation.h"
#import "ZHToast.h"
NS_ASSUME_NONNULL_BEGIN

typedef void(^FinishBlock)(AFHTTPRequestOperation * __nullable operation,id __nullable data);

typedef enum {
    AFFormURLParameterEncoding,
    AFJSONParameterEncoding,
    AFPropertyListParameterEncoding,
} AFHTTPClientParameterEncoding;

#define SESSION_EXPIRED_CODE (401) // token过期码
#define LOGIN_ERROR_CODE (1002) // 登录异常

static inline void verifyKlResponse(AFHTTPRequestOperation *operation,id data, BOOL tip,void(^complete)(NSString  * _Nullable msg)){
    if (operation && !operation.error) {
        if ([data isKindOfClass:NSDictionary.class]) {
            NSString *code = data[@"code"];
            if ([code integerValue] == 1000) {
                // 成功
                if (complete) complete(nil);
            }else if ([code integerValue] == SESSION_EXPIRED_CODE ){//token过期
                // 发送登录通知，切换vc
                notice(@"", nil, @{@"showAlert":@1});
                
                if (complete) complete(data[@"message"]);
//                toast(data[@"msg"]);
            }else{
                if (complete) complete(data[@"message"]);
                if(tip)toast(data[@"message"]);
            }
        }else{
            if (complete) complete(@"数据格式不正确");
            if(tip && NSThread.isMainThread)toastError(@"数据格式不正确");
        }
    }else{
        if (complete) complete(@"请求失败");
        if(tip && !operation.isCancelled){
            dispatch_async(dispatch_get_main_queue(), ^{
                toastError(operation.error.localizedDescription);
            });
        }
    }
}

AFHTTPRequestOperation* request_conv(NSString *url_flag ,id __nullable parmeters, FinishBlock completeBlock);
AFHTTPRequestOperation* uploadFile_request(NSString *url_flag ,id __nullable parmeters, NSString * suf, FinishBlock completeBlock);

@interface BHServiceEngine : NSObject

@end

NS_ASSUME_NONNULL_END
